package com.example.mobileeducationstudentv;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


//class that defines icons for listview and how the layout for quiz listview looks.
public class QuizListAdapter extends ArrayAdapter<String> {
	private final Activity context;
	private final String[] names;
	private final Boolean[] read;

	static class ViewHolder {
		public TextView text;
		public ImageView image;
		public ImageView read;
	}

	public QuizListAdapter(Activity context, String[] names, Boolean[] read) {
		super(context, R.layout.folder_list_item, names);
		this.context = context;
		this.names = names;
		this.read = read;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.folder_list_item, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.text = (TextView) rowView.findViewById(R.id.label);
			viewHolder.image = (ImageView) rowView.findViewById(R.id.icon);
			viewHolder.read = (ImageView) rowView.findViewById(R.id.iconread);
			rowView.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();
		String s = names[position].toLowerCase();
		Boolean b = read[position];
		Boolean iamafolder = false;
		holder.text.setText(s);

		if (s.endsWith(".txt")) {
			holder.read.setVisibility(0);
			holder.image.setImageResource(R.drawable.ic_unknown);
		} else if (!s.contains(".")) {
			holder.image.setImageResource(R.drawable.ic_folder);
			holder.read.setVisibility(4);
			iamafolder = true;
		}

		if (!iamafolder) {
			if (b) {
				holder.read.setVisibility(0);
				holder.read.setImageResource(R.drawable.ic_read);
			} else {
				holder.read.setVisibility(4);
			}
		}
		return rowView;
	}
}